﻿namespace MapleRock.P2PEmulator.RGR
{
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;

  public partial class MainForm : Form, IRgrPageContainer
  {
    private BasicRgrPage[] pages;

    private int currentPageIndex = -1;

    private BasicRgrPage prevPage = null;

    public MainForm()
    {
      this.InitializeComponent();

      this.pages = new BasicRgrPage[] 
      { 
        new IntroductionPage(), 
        new ModelInputParamsPage(), 
        new ExperimentPage(), 
        new AvgDeliveryTimeGraphPage(), 
        new AvgTrafficAmountGraphPage(), 
        new EndPage() 
      };

      this.currentPageIndex = 0;
    }

    private delegate void SetButtonEnabledCallback();

    public void EnableBackwardButton()
    {
      if (this.ForwardButton.InvokeRequired)
      {
        var callback = new SetButtonEnabledCallback(this.EnableBackwardButton);
        this.Invoke(callback);
      }
      else
      {
        this.BackwardButton.Enabled = true;
      }
    }

    public void DisableBackwardButton()
    {
      if (this.ForwardButton.InvokeRequired)
      {
        var callback = new SetButtonEnabledCallback(this.DisableBackwardButton);
        this.Invoke(callback);
      }
      else
      {
        this.BackwardButton.Enabled = false;
      }
    }

    public void EnableForwardButton()
    {
      if (this.ForwardButton.InvokeRequired)
      {
        var callback = new SetButtonEnabledCallback(this.EnableForwardButton);
        this.Invoke(callback);
      }
      else
      {
        this.ForwardButton.Enabled = true;
      }
    }

    public void DisableForwardButton()
    {
      if (this.ForwardButton.InvokeRequired)
      {
        var callback = new SetButtonEnabledCallback(this.DisableForwardButton);
        this.Invoke(callback);
      }
      else
      {
        this.ForwardButton.Enabled = false;
      }
    }

    private void LoadPage()
    {
      if (this.prevPage != null)
      {
        this.prevPage.OnPageLeave();
      }

      var rgrPage = this.pages[this.currentPageIndex];
      this.prevPage = rgrPage;

      rgrPage.RgrPageContainer = this;

      if (this.currentPageIndex == 0)
      {
        this.DisableBackwardButton();
      }
      else 
      {
        this.EnableBackwardButton();
      }

      if (this.currentPageIndex == (this.pages.Length - 1))
      {
        this.DisableForwardButton();
      }
      else
      {
        this.EnableForwardButton();
      }

      this.Text = string.Format(
        "{0} - Страница {1} - Лабораторная работа по дисциплине ОЭВМиС",
        rgrPage.RgrPageName,
        this.currentPageIndex + 1);

      rgrPage.Width = this.contentPanel.Width;
      rgrPage.Height = this.contentPanel.Height;

      this.contentPanel.Controls.Clear();
      this.contentPanel.Controls.Add(rgrPage);

      rgrPage.OnPageEnter();
    }

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      var requestConfirmation = 
        (this.currentPageIndex > 0) && 
        (this.currentPageIndex < (this.pages.Length - 1));
      if (requestConfirmation)
      {
        var result =
          MessageBox.Show("Все несохранённые данные будут потеряны!", "Выход", MessageBoxButtons.OKCancel);
        if (result == DialogResult.Cancel)
        {
          e.Cancel = true;
          return;
        }
      }

      foreach (var page in this.pages)
      {
        page.OnPageUnload();
      }
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
      this.currentPageIndex = 0;
      this.LoadPage();
    }

    private void ForwardButton_Click(object sender, EventArgs e)
    {
      Debug.Assert(
        this.currentPageIndex < (this.pages.Length - 1), 
        "this.currentPageIndex < (this.pages.Length - 1)");

      ++this.currentPageIndex;
      this.LoadPage();
    }

    private void BackwardButton_Click(object sender, EventArgs e)
    {
      Debug.Assert(
        this.currentPageIndex > 0,
        "this.currentPageIndex > 0");

      --this.currentPageIndex;
      this.LoadPage();
    }
  }
}
